package com.pirestupppai.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;

import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.bean.LoginUser;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.update.UpdateManager;
import com.pirestupppai.utils.SharedPreferenceUtil;

import rx.Subscription;
import rx.functions.Action1;

import static com.pirestupppai.rxbus.RxBus.RXBUS_UPDATE;

/**
 * 启动页
 *
 * @author: xiaoluo
 * @date: 2017-03-01 14:29
 */
public class SplashActivity extends BaseActivity {
    private boolean isFirstRun = false;

    private UpdateManager mUpdateManager;

    private Subscription mSubscription;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return 0;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {

    }

    @Override
    protected void initLogic() {
        //注册观察者
        mSubscription = RxBus.getInstance()
                .toObservable(RXBUS_UPDATE, String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        startLogic();
                    }
                });

        mUpdateManager = new UpdateManager(this);
        mUpdateManager.stop();
        mUpdateManager.checkUpdate();

    }

    /**
     * 正式逻辑
     */
    private void startLogic() {
        SharedPreferences sp = SharedPreferenceUtil.getSp();
        SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();

        // 从sp中获取是否第一次启动数据,若无数据,默认为是
        isFirstRun = sp.getBoolean(Constant.SPKey.IS_FIRST_RUN, true);

        // 判断是否第一次启动
        if (isFirstRun) {
            editor.putBoolean(Constant.SPKey.IS_FIRST_RUN, false); // 置为否
            editor.apply();
            startActivity(new Intent(mContext, WelcomeScrollActivity.class));
            finish();
            return;
        }
        String localToken = LoginUser.TokenVerity.getToken();

        if (TextUtils.isEmpty(localToken)) {
            // 本地token为空时,跳转到登录页面
            startActivity(new Intent(mContext, InputPhoneActivity.class));
            finish();
            return;
        }

        // 获取用户数据
        NetworkRequest.getInstance().getUserData(LoginUser.getInstance().getId(), mContext,
                new TupaiSubscriber<TupaiUser>(mContext) {
                    @Override
                    protected void requestSuccess(TupaiUser loginUser) {
                        LoginUser.getInstance().initFromResponse(loginUser);
                        startActivity(new Intent(mContext, MainActivity.class));
                        finish();
                    }

                    @Override
                    protected void requestError(String error) {

                    }
                });

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
